package win.itshen.cloud.shiro.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * mybatis自动填充值配置
 */
@Slf4j
@Component
public class MybatisObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        //新增时 填充的字段
        this.setInsertFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setUpdateFieldValByName("modifyTime", LocalDateTime.now(), metaObject);
        //this.setInsertFieldValByName("createBy", "", metaObject);
        // 初始化删除状态值为0
        this.setInsertFieldValByName("deleted", 0, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新时 需要填充字段
        this.setUpdateFieldValByName("modifyTime", LocalDateTime.now(), metaObject);
        //this.setUpdateFieldValByName("modifyBy", "", metaObject);
    }
}